package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.RiskInvestigationDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.RespUserRegister;
import com.lanlian.commonlib.network.INetWorkCallback;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/6 14:25
 *     desc   : 开户申请结果
 *     version: 1.0
 * </pre>
 */
public class RegisterResultActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mImg;
    private WebView mTvContent;
    private TextView mTvOperate;
    private boolean mIsSuccess;
    private boolean mRiskCompleteState;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_result);
        initView();
        setListeners();
        submit();
    }

    @Override
    protected void initView() {
        super.initView();
        mImgBack = findViewById(R.id.img_back);
        mImg = findViewById(R.id.img);
        mTvContent = findViewById(R.id.tv_content);
        mTvOperate = findViewById(R.id.tv_operate);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvOperate.setOnClickListener(this);
    }

    private void submit() {
        showLoadingDialog("申请中...");
        VolleyRequestUtils.getInstance().userRegister(this.toString(), LLApplication.reqUserRegister, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                mIsSuccess = true;
                RespUserRegister respUserRegister = (RespUserRegister) response;
                mRiskCompleteState = respUserRegister.isRiskCompleteState();
                mImg.setImageResource(R.mipmap.account_img_success);
                if (!TextUtils.isEmpty(respUserRegister.getData())) {
                    mTvContent.loadDataWithBaseURL(null, respUserRegister.getData(),
                            "text/html", "UTF-8", null);
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                dismissLoadingDialog();
                LLApplication.reqUserRegister = null;
                mImg.setImageResource(R.mipmap.account_img_fail);
                mTvContent.loadDataWithBaseURL(null, baseResponse.getMsg(),
                        "text/html", "UTF-8", null);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_operate:
                if (mIsSuccess && !mRiskCompleteState) {
                    new RiskInvestigationDialog(this, new RiskInvestigationDialog.OnClickListener() {
                        @Override
                        public void onWriteClick() {
                            startActivity(new Intent(RegisterResultActivity.this, RiskStateActivity.class)
                                    .putExtra("mobile", LLApplication.reqUserRegister.getMobile()));
                            LLApplication.reqUserRegister = null;
                            finish();
                        }

                        @Override
                        public void onCloseClick() {
                            finish();
                        }
                    }).show();
                } else {
                    finish();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTvContent = null;
    }
}
